package com.pnk087.sample.xml;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Small example class which show how to use the {@link XMLReader}.
 */
public class XMLProcessor {

    public void process() throws IOException, SAXException, ParserConfigurationException {
        XMLReader r = new XMLReader();
        r.addHandler("node", new NodeHandler() {

            public void process(StructuredNode node) {
                try {
                    System.out.println(node.queryString("name"));
                    System.out.println(node.queryValue("price").asDouble(0d));
                    if (!node.isEmpty("resources/resource[@type='test']")) {
                        System.out.println(node
                                .queryString("resources/resource[@type='test']"));
                    }

                    if (!node.isEmpty("resources/resource[@type='test1']")) {
                        System.out.println(node
                                .queryString("resources/resource[@type='test1']"));
                    }
                } catch (XPathExpressionException e) {
                    e.printStackTrace();
                }
            }
        });
        InputStream in = this.getClass().getResourceAsStream("/test.xml");
        r.parse(in);
    }

    public static void main(String[] args) throws Exception {
        XMLProcessor xmlProcessor = new XMLProcessor();
        xmlProcessor.process();
    }
}